package com.zhuifeng.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.fastjson.JSONObject;
import com.zhuifeng.common.core.utils.DateUtils;
import com.zhuifeng.common.core.utils.snow.GuuidUtil;
import com.zhuifeng.common.security.utils.SecurityUtils;
import com.zhuifeng.product.domain.*;
import com.zhuifeng.orders.domain.OrdersDetails;
import com.zhuifeng.product.mapper.ProductMapper;
import com.zhuifeng.product.response.ProductResponse;
import com.zhuifeng.product.service.IProductRuleService;
import com.zhuifeng.product.service.IProductRultAttrCenterService;
import com.zhuifeng.product.service.IProductService;
import com.zhuifeng.utils.ExecutorUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 *
 * @author zhuifeng
 * @date 2022-08-18
 */
@Service
public class ProductServiceImpl implements IProductService
{
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IProductRultAttrCenterService productRultAttrCenterService;

    @Autowired
    private IProductRuleService productRuleService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private final ThreadPoolExecutor executor;

    public ProductServiceImpl() {
        this.executor = ExecutorUtils.getExecutor();
    }

    /**
     * 查询商品
     *
     * @param productId 商品主键
     * @return 商品
     */
    @Override
    public ProductResponse selectProductByProductId(String productId)
    {
        ProductResponse productResponse = productMapper.selectProductByProductId(productId);
        ProductRultAttrCenter productRultAttrCenter = new ProductRultAttrCenter();
        productRultAttrCenter.setProductId(productId);
        productResponse.setProductRuleCenterList(productRultAttrCenterService.selectProductRultAttrCenterList(productRultAttrCenter));
        return productResponse;
    }

    /**
     * 查询商品列表
     *
     * @param product 商品
     * @return 商品
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增商品
     *
     * @param productRequest 商品
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProduct(ProductRequest productRequest)
    {
        String s = String.valueOf(GuuidUtil.getUUID());
        productRequest.setProductId(s);
        productRequest.setCreateBy(SecurityUtils.getStrUserId());
        productRequest.setCreateTime(DateUtils.getNowDate());
        productRequest.setProductTime(DateUtils.getNowDate());
        List<ProductRultAttrCenter> productRultAttrCenterList = productRequest.getProductRuleCenterList();
        productRultAttrCenterList.forEach(productRultAttrCenter -> {
            productRultAttrCenter.setId(GuuidUtil.getUUID() + "");
            productRultAttrCenter.setProductId(s);
            productRultAttrCenter.setCreateBy(SecurityUtils.getStrUserId());
            productRultAttrCenter.setCreateTime(DateUtils.getNowDate());
            productRultAttrCenterService.insertProductRultAttrCenter(productRultAttrCenter);
        });
        return productMapper.insertProduct(productRequest);
    }

    /**
     * 修改商品
     *
     * @param productRequest 商品
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateProduct(ProductRequest productRequest)
    {
        productRequest.setUpdateBy(SecurityUtils.getStrUserId());
        productRequest.setUpdateTime(DateUtils.getNowDate());
        List<ProductRultAttrCenter> productRultAttrCenterList = productRequest.getProductRuleCenterList();
        productRultAttrCenterService.deleteProductRultAttrCenterByProductId(productRequest.getProductId());
        productRultAttrCenterList.forEach(productRultAttrCenter -> {
            productRultAttrCenter.setId(GuuidUtil.getUUID() + "");
            productRultAttrCenter.setProductId(productRequest.getProductId());
            productRultAttrCenter.setCreateBy(SecurityUtils.getStrUserId());
            productRultAttrCenter.setCreateTime(DateUtils.getNowDate());
            productRultAttrCenterService.insertProductRultAttrCenter(productRultAttrCenter);
        });
        return productMapper.updateProduct(productRequest);
    }

    /**
     * 批量删除商品
     *
     * @param productIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductIds(String[] productIds)
    {
        return productMapper.deleteProductByProductIds(productIds);
    }

    /**
     * 删除商品信息
     *
     * @param productId 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductId(String productId)
    {
        return productMapper.deleteProductByProductId(productId);
    }

    /**
     * 修改商品Sku库存数据
     * @param productSku
     */
    @Override
    public void updateProductSkuStock(ProductSku productSku) {
        this.productMapper.updateProductSkuStock(productSku);
    }

    /**
     * 修改商品库存
     * @param ordersDetails
     */
    @Override
    public void updateProductSkuInventory(OrdersDetails ordersDetails) {
        this.productMapper.updateProductSkuInventory(ordersDetails);
    }

    /**
     * 生成订单减库存
     * @param ordersDetails
     */
    @Override
    public void updateProductSkuSubtractInventory(OrdersDetails ordersDetails) {
        this.productMapper.updateProductSkuSubtractInventory(ordersDetails);
    }

    /**
     * 确认收货
     * @param ordersDetails
     */
    @Override
    public void updateProductSkuAffirmReceivingInventory(OrdersDetails ordersDetails) {
        this.productMapper.updateProductSkuAffirmReceivingInventory(ordersDetails);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void productPutaway(String productId) {
        productMapper.updateProductStatus(productId);
        ProductResponse productResponse = productMapper.selectProductByProductId(productId);
        rabbitTemplate.convertAndSend("productPutaway", JSONObject.toJSONString(productResponse));
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void productSold(String productId) {
        productMapper.updateProductStatusSold(productId);
        rabbitTemplate.convertAndSend("productSold", JSONObject.toJSONString(productId));
    }

    @Override
    public Map<String, Object> getProductDetails(String productId) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        CompletableFuture<ProductResponse> product2 = CompletableFuture.supplyAsync(() -> {
            ProductResponse product1 = productMapper.selectProductByProductId(productId);
            stringObjectHashMap.put("product", product1);
            return product1;
        }, executor).thenApplyAsync((product) -> {
            ProductRule productRule = productRuleService.selectProductRuleByTypeId(product.getTypeId());
            stringObjectHashMap.put("productRule",productRule);
            return product;
        }, executor);

        CompletableFuture<Void> productRultAttrCenters1 = CompletableFuture.runAsync(() -> {
            List<ProductRultAttrCenter> productRultAttrCenters = productRultAttrCenterService.selectProductRultAttrCenterByProductId(productId);
            stringObjectHashMap.put("productRultAttrCenters", productRultAttrCenters);
        }, executor);
        try {
            product2.get();
            productRultAttrCenters1.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return stringObjectHashMap;

    }

}
